package org.openkoala.koala.popup.preference;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.openkoala.koala.config.DBConfigService;
import org.openkoala.koala.config.vo.DBConfigVO;
import org.openkoala.koala.exception.KoalaException;

public class DbConfigComponent extends ConfigComponent {

	private DataBaseConfig dataBaseConfig;
	
	private String projectPath;
	
	private boolean isTestDbConfig;
	
	DbConfigComponent(String configName, String projectPath, DBConfigVO dbConfigVO, boolean isTestDbConfig) {
		super(configName);
		this.isTestDbConfig = isTestDbConfig;
		this.projectPath = projectPath;
		dataBaseConfig = new DataBaseConfig(dbConfigVO);
	}

	@Override
	public void createComposite(Composite parent) {
		setComposite(dataBaseConfig.getComposite(parent));
	}

	@Override
	public void doConfig() {
		if (hasVisited()) {
			try {
				if (isTestDbConfig) {
					DBConfigService.updateTestDBConfig(projectPath, dataBaseConfig.getDataBaseConfiguration());
				} else {
					DBConfigService.updateDBConfig(projectPath, dataBaseConfig.getDataBaseConfiguration());
				}
			} catch (KoalaException e) {
				MessageDialog.openError(Display.getCurrent().getActiveShell(), "错误提示", e.getMessage());
				e.printStackTrace();
			}
		}
	}

}
